﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using Dynamic.Core.Utility;
using Dynamic.Core.Extentions;

namespace Dynamic.Core.Types
{
    /// <summary>
    /// 对应数据库表
    /// </summary>
    [DataContract]
    public class Table
    {
        private string idField = Guid.Empty.ToString();
        private string nameField = string.Empty;
        private string descrField = string.Empty;
        private string memoField = string.Empty;
        private string schemaField = "dbo";
        private List<Column> columnsField = new List<Column>();

        /// <summary>
        /// 主键
        /// </summary>
        [DataMember]
        public string ID
        {
            get
            {
                return this.idField;
            }
            set
            {
                this.idField = value;
            }
        }

        /// <summary>
        /// 物理名称
        /// </summary>
        [DataMember]
        public string Name
        {
            get
            {
                return this.nameField;
            }
            set
            {
                this.nameField = value;
            }
        }

        /// <summary>
        /// 类名
        /// </summary>
        [DataMember]
        public string CleanName
        {
            get
            {
                return NamingUtility.GetEntityCalssName(this.nameField);
            }
        }

        /// <summary>
        /// 表描述
        /// </summary>
        [DataMember]
        public string Descr
        {
            get
            {
                if (this.descrField.IsInvalid())
                {
                    return this.Name;
                }
                else
                {
                    return this.descrField;
                }

            }
            set
            {
                this.descrField = value;
            }
        }

        /// <summary>
        /// 备注
        /// </summary>
        [DataMember]
        public string Memo
        {
            get
            {
                return this.memoField;
            }
            set
            {
                this.memoField = value;
            }
        }

        /// <summary>
        /// 数据库架构
        /// </summary>
        [DataMember]
        public string Schema
        {
            get
            {
                return this.schemaField;
            }
            set
            {
                this.schemaField = value;
            }
        }

        /// <summary>
        /// 列
        /// </summary>
        [DataMember]
        public List<Column> Columns
        {
            get
            {
                return this.columnsField;
            }
            set
            {
                this.columnsField = value;
            }
        }
    }
}
